package zuel.innovation.disley.Recruit.controller;

import cn.fabrice.common.pojo.BaseResult;
import cn.fabrice.common.pojo.DataResult;
import cn.fabrice.jfinal.annotation.Param;
import cn.fabrice.jfinal.annotation.ValidateParam;
import cn.fabrice.jfinal.ext.cros.interceptor.CrossInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import zuel.innovation.disley.Recruit.service.RecruitService;
import zuel.innovation.disley.common.module.Recruit;
import zuel.innovation.disley.interceptor.AuthInterceptor;
import zuel.innovation.disley.user.service.UserService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Before(CrossInterceptor.class)
@Path("/recruit")
@ValidateParam


public class RecruitController extends Controller {
    @Inject
    UserService service;
    @Inject
    RecruitService recruitService;


    /**
     * 管理员发布招新内容
     *
     * @param rStart   开始时间
     * @param rEnd     截止时间
     * @param rObject  招新对象
     * @param rSum     招新总人数
     * @param rRequire 招新要求
     * @param rStep    招新步骤
     * @param rTest    招新测试
     * @throws ParseException
     */
    @Param(name = "rStart", required = true)
    @Param(name = "rEnd", required = true)
    @Param(name = "rObject", required = true)
    @Param(name = "rSum", required = true)
    @Param(name = "rRequire", required = true)
    @Param(name = "rStep", required = true)
    @Param(name = "rTest", required = true)
    public void addRecruit(Date rStart, Date rEnd, String rObject, int rSum, String rRequire, String rStep, String rTest) throws ParseException {
        Recruit recruit = new Recruit();
        recruit.setRStart(rStart);
        recruit.setREnd(rEnd);
        recruit.setRObject(rObject);
        recruit.setRSum(rSum);
        recruit.setRRequire(rRequire);
        recruit.setRStep(rStep);
        recruit.setRTest(rTest);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date r_create = df.parse(df.format(System.currentTimeMillis()));
        recruit.setRCreate(r_create);
        recruit.setRDeleted(0);
        renderJson(recruit.save() ? BaseResult.ok() : BaseResult.fail());
    }



    /**
     * 用户获取最新一次招新信息
     */
    @Clear(AuthInterceptor.class)
    public void selectRecruit() {
        List<Recruit> recruit = recruitService.list("selectRecruit");
        if (recruit.isEmpty()) {
            renderJson(BaseResult.fail("暂无招新"));
        } else {
            renderJson(DataResult.data(recruit));
        }
    }

    /**
     * 管理员查看所有招新(分页显示)
     *
     * @param page     页数
     * @param pageSize 每页的信息条数
     */
    @Param(name = "page", required = true)
    @Param(name = "pageSize", required = true)
    public void selectAllRecruits(int page, int pageSize) {
        Page<Record> Recruits = Db.paginate(page, pageSize, "select * ", "from recruit where r_deleted = 0 order by r_create desc");
        if (Recruits.getList().isEmpty()) {
            renderJson(BaseResult.fail("暂无招新信息"));
        } else {
            renderJson(DataResult.data(Recruits));
        }
    }

    /**
     * 模糊搜索招新信息
     */
    @Param(name = "content",required = true)
    @Param(name = "page", required = true)
    @Param(name = "pageSize", required = true)
    public void fuzzySearchRecruit(int page, int pageSize,String content){
        Kv cond= Kv.by("content",content);
        String sql="from recruit where (r_start like '%"+content+"%') or (r_end like '%"+content+"%') or (r_object like '%"+content+"%') or (r_require like '%"+content+"%') or (r_step like '%"+content+"%') or (r_test like '%"+content+"%')";
        Page<Record> Recruits = Db.paginate(page, pageSize, "select * ", sql);
        if (Recruits.getList().isEmpty()) {
            renderJson(BaseResult.fail("查找不到招新信息"));
        } else {
            renderJson(DataResult.data(Recruits));
        }

    }



}
